#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash augustus-3.4.0.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]] && echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]] && echo "请输入正确的安装路径" && exit 1

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
set -e
yum install -y wget tar git gsl-devel
yum install -y cmake libcurl libcurl-devel openssl openssl-devel zlib zlib-devel bzip2 bzip2-devel xz-devel boost-devel bc htslib* gsl* ncurses ncurses-base ncurses-libs ncurses-devel samtools suitesparse-devel mariadb-devel sqlite-devel
wget -t 40 -c -P $buildpath https://github.com/Gaius-Augustus/Augustus/archive/refs/tags/v3.4.0.tar.gz
tar -xzvf $buildpath/v3.4.0.tar.gz -C $buildpath && rm -rf $buildpath/v3.4.0.tar.gz
cd $buildpath/Augustus-3.4.0
sed -i "s/3.3.3/3.4.0/g" common.mk

sed -i "24s%$%& -I$installpath/mysqlpp-3.3.0/include/mysql++%g" src/Makefile
sed -i "24s%$%& -I$installpath/lp_solve_5.5%g" src/Makefile
sed -i "36s%LIBS +=%LIBS += -L$installpath/lp_solve_5.5/lpsolve55/bin/ux64%g" src/Makefile
sed -i "56s%LIBS     +=%LIBS     += -L$installpath/mysqlpp-3.3.0/lib%g" src/Makefile

sed -i "11s%-I/usr/include/bamtools%-I$installpath/bamtools-2.5.2/include/bamtools%g" auxprogs/bam2hints/Makefile
sed -i "12s%LIBS =%LIBS = -L$installpath/bamtools-2.5.2/lib64%g" auxprogs/bam2hints/Makefile

sed -i "11s%/usr/include/bamtools%$installpath/bamtools-2.5.2/include/bamtools%g" auxprogs/filterBam/src/Makefile
sed -i "13s%LIBS =%LIBS = -L$installpath/bamtools-2.5.2/lib64%g" auxprogs/filterBam/src/Makefile
#修改安装路径
sed -i "s%/opt/augustus-\$(AUGVERSION)%${installpath}/augustus-3.4.0%" Makefile
CC=clang CXX=clang++ make -j$(nproc)
make install
rm -rf $buildpath/Augustus-3.4.0
cat >"$installpath/augustus-3.4.0/augustus_modulefiles" <<EOF
#%Module1.0
conflict augustus
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
        set modfile_path [file dirname \$modfile]
            return \$modfile_path
    }
set pwd [getModulefileDir]
set AUGUSTUS \$pwd
setenv AUGUSTUS \$AUGUSTUS
prepend-path PATH \$AUGUSTUS/bin
prepend-path PATH \$AUGUSTUS/scripts
setenv AUGUSTUS_CONFIG_PATH \$AUGUSTUS/config
EOF
